<?php
// $Id$

/**
 * Controller_Articles 控制器
 */
class Controller_Articles extends Controller_Abstract
{
    function actionOne() {
        $condi[] = new QDB_Cond(
            'col_id = ?', Helper_Common::iniFront('tz_column_id')
        );
        $condi[] = new QDB_Cond(
            '[extend_field.fieldname] = ?', 'import'
        );
        $condi[] = new QDB_Cond(
            '[extend_field.fieldvalue] = ?', 'y'
        );

        $articles = Article::find($condi)->limitPage($this->_context->page ,1)->query();
        $records = Article::find($condi)->all()->getCount();
        $this->_view['records'] = $records;
        $this->_view['pagesize'] = 1;
        $this->_view['articles'] = $articles;
    }

    function actionSet() {
        $user = Helper_RBAC::get('user');
        $roles = Helper_RBAC::roles('user');
        $mode = strtolower($this->_context->mode);
        //        dump($role);
        $idset = FUNC::getIdset($this->_context->idset);
        foreach($idset as $id) {
            $record = ArticleExtendField::find('art_id = ? AND fieldname = ?', $id, $mode)->one()->query();
            //            if($record->id()) {
            $record -> fieldvalue = $this->_context->value;
            //            }
            if(!$record->id()) {
                $record -> art_id = $id;
                $record -> fieldname = $mode;
            }
            $record -> save();
            //            $ex_assoc = $record->meta()->Assoc('extend_fields');
            //            $ex_assoc -> on_find_where = array('fieldname'=>$mode);
            //            dump($record);
            //            if($record->user_id == $user->user_id || in_array('ADMIN', $roles)) {
            //
            //            }
        }
    }

    function actionRemove() {
        $user = Helper_RBAC::get('user');
        $roles = Helper_RBAC::roles('user');
        //        dump($role);
        $idset = FUNC::getIdset($this->_context->idset);
        foreach($idset as $id) {
            $record = Article::find('art_id = ?', $id)->one()->query();
            if($record->user_id == $user->user_id || in_array('ADMIN', $roles)) {
                Article::meta()->destroyWhere('art_id = ?', $id);
            }
        }

        $referer = $this->_context->referer() ? $this->_context->referer() : url('');
        return $this->_redirectMessage('网站提示', '删除成功', $referer);
    }

    function actionMore() {
        $user =  Helper_Rbac::get('user');
        //        FUNC::getChildren(Column::getFormCache(null, $this->_context->col_id), $childrens, array('col_id'), false);    //查找孩子
        //        FUNC::getParent(Column::getFormCache(), $this->_context->col_id, $paths, array('name'), false);    //查找父亲
        //        //        dump($parents);
        //        FUNC::formatArrayKey($paths);
        //        if($this->_context->group_id) {
        //            $condi[] = new QDB_Cond(
        //            'role_id = ?', $this->_context->group_id
        //            );
        //            $group = Role::getFormCache(null, $this->_context->group_id);
        ////            dump($group);
        //        }
//        $group = Department::getFormCache(null, $this->_context->depart_id);
//        $column = new Column;
//        $paths = $column->path($this->_context->col_id);
//        $this->_view['paths'] = $paths;
//        $this->_view['group'] = $group;
        $this->_view['user'] = $user;

    }

    function actionIndex()
    {
        $user =  Helper_Rbac::get('user');
        $roles = Helper_RBAC::roles('user');

        if($this->_context->col_id) {
            FUNC::getChildren(Column::getFormCache(null, $this->_context->col_id), $childrens, array('col_id'), false);    //查找孩子
            //        FUNC::getParent(Column::getFormCache(), $this->_context->col_id, $paths, array('name', 'col_id'));    //查找父亲
            //        //        dump($parents);
            //        FUNC::formatArrayKey($paths);
            //dump($paths);
            $childrens[] = $this->_context->col_id;
            FUNC::formatArrayKey($childrens);
            $condi[] = new QDB_Expr(
            'col_id in ( '.implode(',', $childrens).' )'
            );
        }

        if($this->_context->depart_id) {
            $condi[] = new QDB_Cond(
            'depart_id = ?', $this->_context->depart_id
            );
            $group = Department::getFormCache(null, $this->_context->depart_id);
            //            dump($group);
        }

        if(isset($this->_context->keyword)) {
            $condi[] = new QDB_Expr(
            "title like '%{$this->_context->keyword}%'"
            );
        }

        //                dump($condi);
        $page = $this->_context->page;
        $pagesize = Helper_Common::iniFront('article/pagesize');
        //        $pagesize = Q::ini('appini/background/column_list_pagesize');
        $articles = Article::find($condi)->limitPage($page, $pagesize)->order('created DESC')->query();
        $records = Article::find($condi)->all()->getCount('art_id');

        $column = Column::find('col_id = ?', $childrens[0])->one()->query();
        $paths = $column->path($this->_context->col_id);
        $config = $column->getConfig($childrens[0]);
        //                        dump($articles);
        $this->_view['records'] = $records;
        $this->_view['pagesize'] = $pagesize;
        $this->_view['articles'] = $articles;
        if(isset($this->_context->keyword)) {
            $this->_view['paths'] = '搜索结果';
        }
        else {
            $this->_view['paths'] = $paths;
        }
        $this->_view['group'] = $group;
        $this->_view['user'] = $user;
        $this->_view['roles'] = $roles;



        if(in_array('ADMIN', $roles)) $this->_view['admin'] = true;


        //        $this->_viewname = $config->template['viewname']['list'];
    }

    function actionView() {
        $user =  Helper_Rbac::get('user');

//        FUNC::getParent(Column::getFormCache(), $this->_context->col_id, $paths, array('name'), false);    //查找父亲
//        FUNC::formatArrayKey($paths);



        if($this->_context->depart_id) {
            $condi[] = new QDB_Cond(
            'depart_id = ?', $this->_context->depart_id
            );
            $group = Role::getFormCache(null, $this->_context->group_id);
            //            dump($group);
        }


        $condi[] = new QDB_Cond('art_id = ?', $this->_context->art_id);

        //dump($condi);
        $article = Article::find($condi)->one()->query();
        $article->post->meta()->updateDbWhere(new QDB_Expr('views = views + 1'), 'art_id = ?', $this->_context->art_id);

        $v_assoc = $article->meta()->assoc('view_users');
        $v_assoc -> on_find_where = 'user_id='.$user->user_id.' AND art_id='. $this->_context->art_id;

        //        $view_j = Article::find('[view_users.art_id]=?',$user->user_id)->query();
        //dump($view_j);


        if(!$article->view_users->id()) {
            $row = array(
                'user_id' => $user->user_id,
            );
            $vj = new Usersreadarticles($row);
            $article->view_users[] = $vj;
        }

        $article -> save();
        //dump($article->view_users);

        //        dump($article->post->notview_users);


        $column = new Column;
        $config = $column->getConfig($this->_context->col_id);
        $paths = $column->path($this->_context->col_id);
//dump($paths);
        $this->_view['article'] = $article;
        $this->_view['paths'] = $paths;
        $this->_view['group'] = $group;
        if($config->template['viewname']['view']) $this->_viewname = $config->template['viewname']['view'];
    }

    function actionEdit() {
        //dump(Q::ini('appini'));
        $article = Article::find('art_id = ?', $this->_context->art_id)->one()->query();
        if($article -> id()) {
            FUNC::getParent(Column::getFormCache(), $article->col_id, $parents, array('col_id'));    //查找父亲
            FUNC::getChildren(Column::getFormCache(null, $article->col_id), $childrens, array('col_id'));    //查找父亲
            $column = Column::find('col_id = ?', $article->col_id)->one()->query('fields');
        }
        else {
            FUNC::getParent(Column::getFormCache(), $this->_context->col_id, $parents, array('col_id'));    //查找父亲
            FUNC::getChildren(Column::getFormCache(null, $this->_context->col_id), $childrens, array('col_id'));    //查找父亲
            $column = Column::find('col_id = ?', $this->_context->col_id)->one()->query('fields');
        }


        //        unset($parents[0]);
        FUNC::formatArrayKey($parents);
        FUNC::formatArrayKey($childrens);

        //dump($parents);
        //dump($childrens);
        //        dump($column->fields->system->config);
        //        $this->
        dump($column);

        dump($column->getConfig($column->parent_id));

        $this->_view['parents_array'] = $parents;
        $this->_view['childrens_array'] = $childrens;
        $this->_view['subject'] = '发布';
        //        $this->_view['']
        //        if()
        //        $this->_viewname = $column->fields->system->config['template']['viewname'];
    }

    function actionSave() {
        dump($_POST);
    }

}


